import{ useUserInfoStore} from '@/stores/userInfo'
import router from '@/router'
router.beforeEach(async (to, from, next) => {
    const userInfoStore = useUserInfoStore();
    const token = userInfoStore.token;  
    if (token) {
      if (to.path === '/login') {
        next({ path: '/' });
      }else {
        const hasLogin = !!userInfoStore.userInfo.username
        if(hasLogin){
          next()
        }else{
          try {
            await  userInfoStore.getUserInfo()
            next(to)
          } catch (error) {
            console.log(error);
            
             userInfoStore.reset()
            next({ path: '/login' });
          }
        }
      }
    } else {
      if (to.path !== '/login') {
        next({ path: '/login' });
      } else {
        next();
      }
    }
  }); 
router.afterEach(() => {
    
  })
